<?php
//
// +----------------------------------------------------------------------+
// |zen-cart Open Source E-commerce                                       |
// +----------------------------------------------------------------------+
// | Copyright (c) 2003 The zen-cart developers                           |
// |                                                                      |
// | http://www.zen-cart.com/index.php                                    |
// |                                                                      |
// | Portions Copyright (c) 2003 osCommerce                               |
// +----------------------------------------------------------------------+
// | This source file is subject to version 2.0 of the GPL license,       |
// | that is bundled with this package in the file LICENSE, and is        |
// | available through the world-wide-web at the following url:           |
// | http://www.zen-cart.com/license/2_0.txt.                             |
// | If you did not receive a copy of the zen-cart license and are unable |
// | to obtain it through the world-wide-web, please send a note to       |
// | license@zen-cart.com so we can mail you a copy immediately.          |
// +----------------------------------------------------------------------+
// $Id: categories_ul_generator.php 2004-07-11  DrByteZen $
//      based on site_map.php v1.0.1 by networkdad 2004-06-04
// Fix for line 48 provided by Paulm, uploaded by Kelvyn
// Changes for click-show-hide menu by Cameron, 2008-01-10

// Showing category counts will use default Zen function, which generates massive
// recusive database queries. Could be improved by instead retrieving in a single
// query all products to categories and then using recursive PHP to fetch counts.

class zen_categories_ul_generator {
  var $root_category_id = 0,
      $max_level = 6,
      $data = array(),
      $root_start_string = '',
      $root_end_string = '',
      $parent_start_string = '',
      $parent_end_string = '',
      $parent_group_start_string = '%s<ul class="dropdown">',
      $parent_group_end_string = '%s</ul>',
      $child_start_string = '%s<li>',
      $child_end_string = '%s</li>',
      $spacer_string = '',
	  $count = 0,
      $spacer_multiplier = 1;
  var $document_types_list = ' (3) ';  // acceptable format example: ' (3, 4, 9, 22, 18) '

  function zen_categories_ul_generator() {
    global $languages_id, $db, $request_type;
    $this->server    = ((ENABLE_SSL == true && $request_type == 'SSL') ? HTTPS_SERVER : HTTP_SERVER);
    $this->base_href = ((ENABLE_SSL == true && $request_type == 'SSL') ? DIR_WS_HTTPS_CATALOG : DIR_WS_CATALOG);
    $this->data = array();
    $categories_query = "select c.categories_id, cd.categories_name, c.parent_id from " . TABLE_CATEGORIES . " c, " . TABLE_CATEGORIES_DESCRIPTION . " cd " .
                        "where c.categories_id = cd.categories_id and c.categories_status=1  and cd.language_id = '" . (int)$_SESSION['languages_id']  . "'" .
                        "order by c.parent_id, c.sort_order, cd.categories_name";
    $categories = $db->Execute($categories_query);
    while (!$categories->EOF) {
      $products_in_category = (SHOW_COUNTS == 'true' ? zen_count_products_in_category($categories->fields['categories_id']) : 0);
      $this->data[$categories->fields['parent_id']][$categories->fields['categories_id']] = array('name' => $categories->fields['categories_name'], 'count' => $products_in_category);
      $categories->MoveNext();
    }
// DEBUG: These lines will dump out the array for display and troubleshooting:
// foreach ($this->data as $pkey=>$pvalue) { 
//   foreach ($this->data[$pkey] as $key=>$value) { echo '['.$pkey.']'.$key . '=>' . $value['name'] . '<br>'; }
// }
  }

  function buildBranch($parent_id, $level = 0, $cpath = '') {
    global $cPath;
    //$result = "\n".sprintf($this->parent_group_start_string, str_repeat(' ', $level*4))."\n";
    if (isset($this->data[$parent_id])) {
	  
	  $length = count($this->data[$parent_id]);
	  $i = 0;
      foreach ($this->data[$parent_id] as $category_id => $category) {
		
			$cat = array();
			zen_get_parent_categories($cat, $category_id);
			$cat_count = count($cat);
			if($cat_count==0 && rev_has_category_subcategories($category_id)){
				$result .= '<li class="dropdown level'.$level.'">';
			}else{
				if(rev_has_category_subcategories($category_id)){
					$result .= '<li class="dropdown-submenu level'.$level.'">';
				}else{
					$result .= '<li class="level'.$level.'">';
				}
			}
		//$result .= sprintf($this->child_start_string, str_repeat(' ', $level*4+2));

        
        if ($level == 0) {
          $result .= $this->root_start_string;
          $new_cpath  = $category_id;
        } else {
          $new_cpath = $cpath."_".$category_id;
        }

		$cat = array();		$path_array = explode('_', $cPath);
		zen_get_parent_categories($cat, $category_id);
		$cat_count = count($cat);
		if($cat_count==0 && rev_has_category_subcategories($category_id)){
			if(in_array($category_id, $path_array)){
				$result .= '<a class="dropdown-toggle dd-active" data-toggle="dropdown" href="' . zen_href_link(FILENAME_DEFAULT, 'cPath=' . $new_cpath) . '">'.$category['name'].'</a>';
            }else{
				$result .= '<a class="dropdown-toggle" data-toggle="dropdown" href="' . zen_href_link(FILENAME_DEFAULT, 'cPath=' . $new_cpath) . '">'.$category['name'].'</a>'; 
            }
		}else{	
			if(rev_has_category_subcategories($category_id)){
				if(in_array($category_id, $path_array)){
					$result .= '<a href="' . zen_href_link(FILENAME_DEFAULT, 'cPath=' . $new_cpath) . '" class="dd-active">'.$category['name'].'</a>';					
				}else{
					$result .= '<a href="' . zen_href_link(FILENAME_DEFAULT, 'cPath=' . $new_cpath) . '">'.$category['name'].'</a>'; 
				}
			}else{
				if(in_array($category_id, $path_array)){
					$result .= '<a href="' . zen_href_link(FILENAME_DEFAULT, 'cPath=' . $new_cpath) . '" class="dd-active">'.$category['name'].'</a>';			
				}else{
					$result .= '<a href="' . zen_href_link(FILENAME_DEFAULT, 'cPath=' . $new_cpath) . '">'.$category['name'].'</a>';			
				}
			}
		}
		
		
		$cat = array();
		zen_get_parent_categories($cat, $category_id);
		$cat_count = count($cat);
		if($cat_count==0){
			$result .= '';
		}else{
			if(rev_has_category_subcategories($category_id)){
				$result .= '<ul class="dropdown-menu" id="catHead_'.$category_id.'">';
			}else{
				$result .= '';
			}
		}
		
		$cat = array();
		zen_get_parent_categories($cat, $category_id);
		$cat_count = count($cat);
		if($cat_count==0 && rev_has_category_subcategories($category_id)){
			$result .= '<ul class="dropdown-menu" id="catHead_'.$category_id.'">';
		}else{
			$result .= '';
		}
        if ($level == 0) {
          $result .= $this->root_end_string;
        }
        if (isset($this->data[$category_id])) {
          $result .= $this->parent_end_string;
        }
        if (isset($this->data[$category_id]) && (($this->max_level == '0') || ($this->max_level > $level+1))) {

          $result .= $this->buildBranch($category_id, $level+1, $new_cpath);
		  		
				$cat = array();
				zen_get_parent_categories($cat, $category_id);
				$cat_count = count($cat);
				if($cat_count==0){
					$result .= '';
				}else{
					if(rev_has_category_subcategories($category_id)){
						$result .= '</ul>';
					}else{
						$result .= '';
					}
				}
				
		$cat = array();
		zen_get_parent_categories($cat, $category_id);
		$cat_count = count($cat);
		if($cat_count==0){
			$result .= '</ul>';
		}else{
			$result .= '';
		}
				
          $result .= sprintf($this->child_end_string, str_repeat(' ', $level*4+2))."\n";
		  		
        } else {
          $result .= sprintf($this->child_end_string, '')."\n";
        }
		

		if(defined('REVELATION_CATEGORIES')){
			if($i >= (int)REVELATION_CATEGORIES){
				break;
			}
		}else{
			if($i >= 7){
				break;
			}
		}
		$i++;
      }
    }
    //$result .= sprintf($this->parent_group_end_string, str_repeat(' ', $level*4))."\n";
    return $result;
  }

  function buildTree() {
    return $this->buildBranch($this->root_category_id, 0);
  }
}
?>
